<?php
namespace apiadmin\modules\v1\controllers\admin;

use common\models\BalanceLog;
use apiadmin\modules\v1\controllers\CoreController;
use apiadmin\modules\v1\controllers\finance\RechargeController;
use common\models\EthTradeLog;
use common\models\GroupFinalist;
use common\models\GroupWinning;
use common\models\Member;
use common\models\StaticInstance;
use common\models\TradeCommodity;
use common\models\WithdrawLog;

class IndexController extends CoreController
{
    public function actionListInfo(){
        $count_recharge = EthTradeLog::find()->sum('value');
        $data['count_recharge'] = $count_recharge;
        $data['count_withdraw'] = WithdrawLog::find()->where(['state'=>4])->sum('money');
        $data['count_fee']      = WithdrawLog::find()->where(['state'=>4])->sum('fee');
        $data['count_user']     = Member::find()->count();
        $data['add_user']       = Member::find()->where(['between', 'create_time',
            strtotime(date('Y-m-d 00:00:00',time())), time()])->count();

        $data['winning']        = GroupWinning::find()->where(['between', 'winning_time',
            date('Y-m-d 00:00:00',time()), date('Y-m-d H:i:s',time())])->count();

        $data['today_recharge'] = EthTradeLog::find()->where(['between', 'create_time',
            strtotime(date('Y-m-d 00:00:00',time())), time()])->sum('value');

        $data['today_withdraw'] = WithdrawLog::find()->where(['between', 'create_time',
            strtotime(date('Y-m-d 00:00:00',time())), time()])->andwhere(['state'=>4])->sum('money');

        $data['today_fee'] = WithdrawLog::find()->where(['between', 'create_time',
            strtotime(date('Y-m-d 00:00:00',time())), time()])->andwhere(['state'=>4])->sum('fee');

        $data['day_finalist'] = 0;
        $data['finalist_user'] = 0;
        $group_finalist = GroupFinalist::getAll(['between', 'finalist_time',
            date('Y-m-d 00:00:00',time()), date('Y-m-d H:i:s',time())],'commodity_id,group_money,member_id');

        if ($group_finalist) {
            $commodity_id = array_unique(array_column($group_finalist, 'commodity_id'));
            $commodityArr = TradeCommodity::getAll(['commodity_id' => $commodity_id], 'commodity_id,finalist_income');

            foreach ($commodityArr as $k => $v) {
                $commodityInfo[$v['commodity_id']] = $v['finalist_income'];
            }

            foreach ($group_finalist as $fk => $fv) {
                $data['day_finalist'] += ($fv['group_money'] * $commodityInfo[$fv['commodity_id']]??0) / 100;
            }
            $data['finalist_user'] = count(array_unique(array_column($group_finalist,'member_id')));
        }
        /*今日新增合约*/
        $data['day_commodity']= StaticInstance::find()->where(['between', 'create_time',
            strtotime(date('Y-m-d 00:00:00',time())), time()])->sum('in_num');
        
        /*全部合约*/
        $data['count_commodity']= StaticInstance::find()->sum('in_num');
        $data['count_commodity'] = $data['count_commodity'] -184600;
        /*全部入围收益*/
        $data['count_finalist'] =BalanceLog::find()->where(['balance_type'=>4,'source_type'=>22])->sum('balance');
        /*全部奖金*/
        $data['count_bonus'] =BalanceLog::find()->where(['balance_type'=>4,'source_type'=>[8,9,10,17,19,22]])->sum('balance');
      $this->out('菜单展示',$data);
    }
}